import 'dart:async';

import "package:get/get.dart";

import '../../../../apis/repairs.dart';
import '../../../../components/status/toast.dart';
import '../../../../controller/gather_ctrl.dart';
import '../../../../controller/network/bridge.dart';
import '../../../../controller/network/network_ctrl.dart';
import '../../../../model/bean/repairs_list_bean.dart';

class RepairsDetailsCtrl extends GatherCtrl {
  final type = 0.obs;

  // 查看网络状态
  final _nc = Get.find<NetworkCtrl>();

  // 标记符号（用于检查请求完成后无数据组件与数据组件之的切换）
  final MARKSYMBOL = false.obs;

  // 组件是否初始化完成
  RxBool isInitComplete = false.obs;

  // 请求参数
  final Map<String, dynamic> params = {
    "id": "",
  };

  // 页面组件
  final pageData = RepairsBean().obs;

  @override
  void onInit() {
    // 创建网络变化观察
    Bridget.createNetworkCtrl(this);

    // 获取路由参数
    params["id"] = Get.arguments["id"];

    // -
    type.value = Get.arguments["type"];

    // 初始化页面
    readPageDataList();
  }

  @override
  void onClose() {
    // 移除观察
    Bridget.delNetworkCtrl(this);
  }

  @override
  void modify() {
    if (_nc.networkStatus.value) {
      readPageDataList();
    }
  }

  /// 读取界面数据
  /// * [isToast] bool|是否展示动画框|选填|默认值：false
  /// * [isReset] bool|是否清空数据|选填|默认值：false
  /// * [callback] Function|回调函数|选填
  void readPageDataList({
    bool isToast = false,
    bool isReset = false,
    Function callback = null,
  }) async {
    // 开启轻提示
    if (isToast) Toast.loading(title: '正在读取');

    // 请求数据
    RepairsBean result = await Repairs.queryRepairsDetails(params);

    // 验证结果
    if (result != null) {
      // 赋值
      pageData.value = result;

      MARKSYMBOL.value = true;
    }

    // 标记加载完成
    isInitComplete.value = true;
  }

  /// 修改维修状态
  void changeRepairsStatus() async {
    Toast.loading(title: "正在提交...");
    try {
      var result = await Repairs.changeStatus({"id": params["id"]});
      Toast.success(title: result);
      Timer(Duration(seconds: 2), () {
        // 关闭动画
        Toast.closeToast();
        Get.back(result: true);
      });
    } catch (e) {
      print(e);
      // 关闭动画
      Toast.fail(title: "操作异常");
    }
  }
}
